<?php

namespace App\Models;

use Log;
use App\Cache\BaseCache;

class Resource extends BaseModel {
    //表明
    protected $table = 'resource';
    //主键
    protected $primaryKey = 'id';
    
    //
    protected function getResourceObejct($id, $model = null) {
        $resource = $this->getResourceModel($id, $model);
        if (isset($resource->id) && $resource->is_delete != 1) {
            $resourceObject = array();
            $resourceObject['resourceId'] = $resource->id;
            $resourceObject['type'] = 1;
            $resourceObject['countDown'] = $resource->duration;
            $resourceObject['key'] = $resource->res; //资源json
            $resourceObject['avWidth'] = 100;
            $resourceObject['avHeight'] = 100;
            $resourceObject['avSize'] = 100;
            $resourceObject['avDuration'] = 2;
            $resourceObject['avPoster'] = '';
            return $resourceObject;
        }
        return;
    }

    //获取
    public function getResourceModel($id, $resourceModel = '') {
        $modelCache = new BaseCache();
        $modelCache->method = 'get';
        $modelCache->cacheName = 'resourceModel';
        $modelCache->cachePar = array($id); //集合
        $retCache = $modelCache->HandleCache();
        if ($retCache !== 'NO_CACHE') {
            if (!isset($retCache->id)) {
                return;
            }
            return $retCache;
        }
        if (isset($resourceModel->id) && $resourceModel->id > 0) {
            $resource = $resourceModel;
        } else if ($id > 0) {
            $resource = $this->selectselect('id','res','duration','link','skip','skip_time','type','is_delete')
                    ->where('id', '=', $id)
                    ->first();
        } else {
            $resource = 'CACHE_BREAKDOWN';
        }
        $modelCache->method = 'put';
        $modelCache->cacheValue = $resource;
        $modelCache->HandleCache();
        if (!isset($resource->id)) {
            return;
        }
        return $resource;
    }

    //获取
    public function getStartupHomepage() {
        $modelCache = new BaseCache();
        $modelCache->method = 'get';
        $modelCache->cacheName = 'startupHomepage';
        $retCache = $modelCache->HandleCache();
        if ($retCache === 'CACHE_BREAKDOWN') {
            return array();
        }
        $data = array();
        if (isset($retCache['resourceId'])) {
            $data = $retCache;
        } else {
            $resource = $this->select('id','res','duration','link','skip','skip_time','type','is_delete')
                    ->where('type', '=', 1)
                    ->where('is_delete', '=', 0)
                    ->first();
            if(isset($resource->id)){
                $data = $resource->getResourceObejct($resource->id,$resource);
                $modelCache->cacheValue = $data;
            } else {
                $modelCache->cacheValue = 'CACHE_BREAKDOWN';
            }
            $modelCache->method = 'put';
            $modelCache->HandleCache();
        }
        return $data;
    }

}
